//
//  RouteTableViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/3/25.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "RouteDetailTableViewController.h"
#import "RouteTableViewController.h"
#import "UIImageView+WebCache.h"
#import "RouteTableViewCell.h"
#import "CDHttpHelper.h"
#import "RouteModel.h"
@interface RouteTableViewController ()
@property(nonatomic,assign)CGFloat widthOfRow;
@property(nonatomic,strong)NSMutableArray * allDataArray;
@end
#define url @"https://chanyouji.com/api/destinations/plans/%ld.json?page=1 "
@implementation RouteTableViewController

- (void)viewDidLoad {
    /*
     
     攻略详情下级行程界面
     
     */
    
    [super viewDidLoad];
    
    self.navigationItem.title = [self.name stringByAppendingString:@"行程"];
    self.allDataArray = [NSMutableArray array];
    [self.tableView registerClass:[RouteTableViewCell class] forCellReuseIdentifier:@"route"];
   
    [self handleData];
}

- (void)handleData{
    [[CDHttpHelper defaultCDHttpHelper] get:[NSString stringWithFormat:@"https://chanyouji.com/api/destinations/plans/%ld.json?page=1",self.ID] params:nil success:^(id responseObj) {
       NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dict in array) {
            RouteModel *routeModel = [RouteModel new];
            [routeModel setValuesForKeysWithDictionary:dict];
            [self.allDataArray addObject:routeModel];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"route" forIndexPath:indexPath];
    RouteModel *model = self.allDataArray[indexPath.row];
    cell.titlelabel.text = model.name;
    cell.destinationLabel.text = [[NSString stringWithFormat:@"%ld 天/",model.plan_days_count] stringByAppendingString:[NSString stringWithFormat:@"%ld 旅行地",model.plan_nodes_count]];
    cell.discriptionLabel.text = model.descriptions;
    cell.discriptionLabel.frame = CGRectMake(15, CGRectGetMaxY(cell.backImageView.frame), [UIScreen mainScreen].bounds.size.width - 30, [self getTrueHeightWithDescription:model.descriptions]);
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
    
    self.widthOfRow = cell.backImageView.frame.size.height + 10;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RouteModel *model = self.allDataArray[indexPath.row];
    return [self getTrueHeightWithDescription:model.descriptions] + self.widthOfRow;
}


- (CGFloat)getTrueHeightWithDescription:(NSString *)discuss
{
    CGRect rect = [discuss boundingRectWithSize:(CGSizeMake([UIScreen mainScreen].bounds.size.width, 200000)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil];
    return rect.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RouteDetailTableViewController *routeDetailVC = [[RouteDetailTableViewController alloc]init];
    RouteModel *model = self.allDataArray[indexPath.row];
    routeDetailVC.id = model.id;
    [self.navigationController pushViewController:routeDetailVC animated:YES];
}



@end
